//microcommand define oeis.bfile
signature("<a-number>")
before(({ session, options, args }) => {
  if (options.help) return
  if (args.length < 1) return session.i18n("internal.insufficient-arguments")
  if (args.length > 1) return session.i18n("internal.redunant-arguments")
  if (!args[0].match(/^A?\d{6}$|^[1-9]\d{0,5}$/i)) return session.i18n("internal.invalid-argument", ["a-number", session.i18n("internal.check-syntax")])
})
action(async ({ root }, aNumber) => {
  const number = parseInt(aNumber.replace(/^A/i, ""))
  const numberPadded = number.toString().padStart(6, "0")
  const data = await ctx.http.get(`https://oeis.org/A${numberPadded}/b${numberPadded}.txt`, { responseType: "text" })

  const lines = data.trim().split("\n").filter(line => line.match(/^\d+ \d+$/))
  const list = lines.map(line => line.split(" ", 2)[1].trim() + "n")
  const expr = `[${"NaN,".repeat(parseInt(lines[0].split(" ", 1)[0]))}${list.join(",")}]`

  let displayData = root ? data.split("\n", 15).join("\n") : data
  if (displayData.length < data.length) displayData += "\n..."
  return h("rpipeline:expr", { data: expr }, displayData)
})
locale("zh-CN", {
  description: "从整数数列在线百科全书获取 b-file",
  messages: {
    "invalid-input": "输入无效…",
  },
})